package model;

import entity.Class;
import entity.Province;
import entity.Student;
import iface.IModel;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author Noi
 */
public class StudentTableModel extends AbstractTableModel implements IModel<Student> {

    private manager.Manager manager;

    public StudentTableModel(manager.Manager manager) {
        this.manager = manager;
    }

    @Override
    public int getRowCount() {
        int count = 0;
        for (int i = 0; i < this.manager.getStudentManager().size(); i++) {
            if (this.manager.getStudentManager().get(i).isStatus()) {
                count++;
            }
        }
        return count;
    }

    @Override
    public int getColumnCount() {
        return 4;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Class classObj = null;
        Province province = null;
        Student student = this.manager.getStudentManager().get(rowIndex);

        if (student.isStatus() == true) {
            classObj = this.manager.getClassManager().getById(student.getClassID());
            province = this.manager.getProvinceManager().getByID(student.getProvinceID());
            String gender = student.isGender() ? "Nam" : "Nu";
            switch (columnIndex) {
                case 0:
                    return student.getStudentID();
                case 1:
                    return student.getStudentName();
                case 2:
                    return classObj;
                case 3:
                    return gender;
                default:
                    return null;
            }
        } else {
            return null;
        }
    }

    @Override
    public String getColumnName(int column) {
        switch (column) {
            case 0:
                return "Mã Học Sinh";
            case 1:
                return "Tên Học Sinh";
            case 2:
                return "Tên Lớp";
            case 3:
                return "Giới Tính";
            default:
                return null;
        }
    }
    
    @Override
    public void add(Student obj) {
        this.manager.getStudentManager().add(obj);
        this.fireTableDataChanged();
    }

    @Override
    public void replace(int index, Student obj) {
        this.manager.getStudentManager().replace(index, obj);
        this.fireTableDataChanged();
    }

    @Override
    public void refresh() {
        this.fireTableDataChanged();
    }

    @Override
    public void remove(Student obj) {
        this.manager.getStudentManager().remove(obj);
        this.fireTableDataChanged();
    }

    @Override
    public Student get(int i) {
        int j = 0;
        for (int k = 0; k < this.manager.getStudentManager().size(); k++) {
            if (this.manager.getStudentManager().get(k).isStatus()) {
                if (i == j) {
                    return this.manager.getStudentManager().get(k);
                }
                j++;
            }
        }
        return null;
    }
}
